import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report

# 1. 数据加载
data = pd.read_csv('../demo/reviews.csv')  # 假设数据在reviews.csv中

# 2. 数据预处理
X = data['text']
y = data['label']

# 3. 文本向量化
vectorizer = TfidfVectorizer()
X_vec = vectorizer.fit_transform(X)

# 4. 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X_vec, y, test_size=0.2, random_state=42)

# 5. 训练模型
model = MultinomialNB()
model.fit(X_train, y_train)

# 6. 模型评估
y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))